<?php

class Proxy {

	var $url = "";
	public $urlToLoad = "";
	public $page = "";
	var $error = "";

	function Proxy($purl) {

		if ($purl) {

			$this->url = $purl;

			$urlSpit = explode("/", $this->url);

			$this->urlToLoad = $this->makingUrl($this->cleanUrl($urlSpit));

			$this->getPages();
		} else {

			$this->error = "There is no URL.";
		}
	}

	function getPages() {

		$opts = array(//por la belleza de su contenido en el silencio
			'socket' => array(
				'bindto' => '0:7000',
			),
		);

		$context = stream_context_create($opts);

		$page = file_get_contents($this->urlToLoad, false, $context);

		$this->bodyAnclas($page);
	}

	function cleanUrl($purlSplit) {

		$newUrlArr = array();

		$cont = -1;

		for ($i = 0; $i <= count($purlSplit) - 1; $i++) {

			if ($purlSplit[$i]) {

				$newUrlArr[++$cont] = $purlSplit[$i];
			}
		}

		if ((strpos($newUrlArr[$cont], '.') || strpos($newUrlArr[$cont], '?')) && // removing parameters and extensions
				!( substr_count($newUrlArr[$cont - 1], 'http://') || substr_count($newUrlArr[$cont - 1], 'https://') )) {

			$auxUrlArray = array();

			for ($i = 0; $i <= $cont; $i++) {

				$auxUrlArray[$i] = $newUrlArr[$i];
			}

			$newUrlArr = $auxUrlArray;
		}

		return $newUrlArr;
	}

	function makingUrl($purlSplit) {

		$returnUrl = "";

		foreach ($purlSplit as $key => $value) {

			if ($value == 'http:' || $value == 'htpps:')
				$returnUrl .= $value . '//';
			else
				$returnUrl .= $value . '/';
		}

		return $returnUrl;
	}

	function bodyAnclas($page) {

		//$page = 'title="Google" align="left" id="hplogo" onload="window.lol&amp;&amp;lol()" style="background:url(images/srpr/logo11w.png) no-repeat;background-size:269px 95px;height:95px;width:269px"><div nowrap="" style="color:#777;font-size:16px;font-weight:bold;position:relative;lef';


		$patron = array('/url\(\'http:\/\//', '/url\("http:\/\//', '/url\(\'/', '/url\("/', '/url\(\//', //'/url\(/',
			'/cssUrlHttpSQ/', '/cssUrlHttpDQ/', '/cssUrlSQ/', '/cssUrlDQ/', '/cssUrlWS/');
		$patronV = array('cssUrlHttpSQ', 'cssUrlHttpDQ', 'cssUrlSQ', 'cssUrlDQ', 'cssUrlWS',// 'url(' . $this->urlToLoad,
			'url(\'http://', 'url("http://', 'url(\'', 'url("', 'url(' . $this->urlToLoad);

		$page = preg_replace($patron, $patronV, $page);

		$patron = array('/throw\(\'http:\/\//', '/throw\("http:\/\//', '/throw\(\'/', '/throw\("/', '/throw\(\//', '/throw\(/',
			'/cssUrlHttpSQ/', '/cssUrlHttpDQ/', '/cssUrlSQ/', '/cssUrlDQ/', '/cssUrlWS/');
		$patronV = array('cssUrlHttpSQ', 'cssUrlHttpDQ', 'cssUrlSQ', 'cssUrlDQ', 'cssUrlWS', 'throw(' . $this->urlToLoad,
			'throw(\'http://', 'throw("http://', 'throw(\'', 'throw("', 'throw(' . $this->urlToLoad);

		$page = preg_replace($patron, $patronV, $page);

		$patron = array('/src=\'\//', '/src="\//', '/href=\'\//', '/href="\//', '/url\(\'\//', '/url\("\//',
			'/ content=\'\//', '/ content="\//');

		$patronV = array('src=\'' . $this->urlToLoad . '', 'src="' . $this->urlToLoad . '', 'href=\'' . $this->urlToLoad . '',
			'href="' . $this->urlToLoad . '', 'url(\'' . $this->urlToLoad . '', 'url("' . $this->urlToLoad,
			' content=\'' . $this->urlToLoad . '', ' content="' . $this->urlToLoad . '');

//echo "<strong>changed ...=['|\"] por ...=['|\"]url</strong><br/>";
		$page = preg_replace($patron, $patronV, $page);
//echo $newHtml.'<br/><br/>';//Primera iteracion-------------------------

		$patron1 = array('/src=\'http:\/\//', '/src="http:\/\//', '/src=\'https:\/\//', '/src="https:\/\//',
			'/href=\'http:\/\//', '/href="http:\/\//', '/href=\'https:\/\//', '/href="https:\/\//',
			'/url\(\'http:\/\//', '/url\("http:\/\//', '/url\(\' http:\/\//', '/url\(" http:\/\//',
			'/ content=\'http:\/\//', '/ content="http:\/\//', '/ content=\' http:\/\//', '/ content=" http:\/\//');

		$patron1V = array('srcHttpOffSQ', 'srcHttpOffDQ', 'srcHttpsOffSQ', 'srcHttpsOffDQ',
			'hrefHttpOffSQ', 'hrefHttpOffDQ', 'hrefHttpsOffSQ', 'hrefHttpsOffDQ',
			'cssUrlSQ', 'cssUrlDQ', 'cssUrlSSQ', 'cssUrlSDQ',
			'metaContentSQ', 'metaContentDQ', 'metaContentSSQ', 'metaContentSDQ');

		$page = preg_replace($patron1, $patron1V, $page);

//echo "<strong>Cambiar ...=['|\"]http(s)://' por algun token</strong><br/>";
//echo $newHtml.'<br/><br/>';//Segunda iteracion-------------------------

		$patron2 = array('/src=\'/', '/src="/', '/href=\'/', '/href="/', '/url\(\'/', '/url\("/', '/ content=\'/', '/ content="/',
			'/srcHttpOffSQ/', '/srcHttpOffDQ/', '/srcHttpsOffSQ/', '/srcHttpsOffDQ/',
			'/hrefHttpOffSQ/', '/hrefHttpOffDQ/', '/hrefHttpsOffSQ/', '/hrefHttpsOffDQ/',
			'/cssUrlSQ/', '/cssUrlDQ/', '/cssUrlSSQ/', '/cssUrlSDQ/',
			'/metaContentSQ/', '/metaContentDQ/', '/metaContentSSQ/', '/metaContentSDQ/');

		$patron2V = array('src=\'' . $this->urlToLoad . '', 'src="' . $this->urlToLoad . '', 'href=\'' . $this->urlToLoad . '', 'href="' . $this->urlToLoad . '',
			'url(\'' . $this->urlToLoad . '', 'url("' . $this->urlToLoad . '', ' content=\'' . $this->urlToLoad . '', ' content="' . $this->urlToLoad . '',
			'src=\'http://', 'src="http://', 'src=\'https://', 'src="https://',
			'href=\'http://', 'href="http://', 'href=\'https://', 'href="https://',
			'url(\'http://', 'url("http://', 'url(\'https://', 'url("https://',
			' content=\'http://', ' content="http://', ' content=\' http://', ' content=" http://');

//echo "<strong>Cambiar ...=['|\"]dir/' por ...=['|\"]url/dir y devolver los valores de los tokens</strong><br/>";
		$this->page = preg_replace($patron2, $patron2V, $page);
	}

}

$proxy = new Proxy($_GET['url']);
echo '	    
        <script type="text/javascript" src="../js/jQuery-1.9.1.js"></script>
        <script type="text/javascript" src="../js/jquery-ui-1.10.3.custom.min.js"></script>
		<script type="text/javascript" src="../js/jwebsocket.js"></script>
		<script type="text/javascript" src="../js/js_code.js"></script>
		<script type="text/javascript" src="../js/GlobisPlugin.js"></script>
		<script type="text/javascript" src="../js/filterProxy.js"></script>		
		<script type="text/javascript" src="../js/common.js"></script>	
		<div id="pageLoad" > ' . $proxy->page . ' </div> 
		  
';
?>

